﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SaleOfMoviesSystem.Sqlite.Views
{
    public partial class FrmMain : Form
    {
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect,
            int nTopRect,
            int nRightRect,
            int nBottomRect,
            int nWidthEllipse,
            int nHeightEllipse

        );

        public FrmMain()
        {
            InitializeComponent();
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 25, 25));
            pnlNav.Height = btnHome.Height;
            pnlNav.Top = btnHome.Top;
            pnlNav.Left = btnHome.Left;
            btnHome.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Home());
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
        }

        private void btnHome_Click(object sender, EventArgs e)
        {
            pnlNav.Height = btnHome.Height;
            pnlNav.Top = btnHome.Top;
            pnlNav.Left = btnHome.Left;
            btnHome.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Home());
            //this.tbHome.Visible = true;
        }

        private void btnHome_Leave(object sender, EventArgs e)
        {
            btnHome.BackColor = Color.FromArgb(24, 30, 54);
        }

        private void btnFilm_Click(object sender, EventArgs e)
        {
            pnlNav.Height = btnFilm.Height;
            pnlNav.Top = btnFilm.Top;
            pnlNav.Left = btnFilm.Left;
            btnFilm.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Film());
            //this.tbFilm.Visible = true;
        }

        private void btnFilm_Leave(object sender, EventArgs e)
        {
            btnHome.BackColor = Color.FromArgb(24, 30, 54);
            btnFilm.BackColor = Color.FromArgb(24, 30, 54);
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            pnlNav.Height = btnShow.Height;
            pnlNav.Top = btnShow.Top;
            pnlNav.Left = btnShow.Left;
            btnShow.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Show());
        }

        private void btnShow_Leave(object sender, EventArgs e)
        {
            btnHome.BackColor = Color.FromArgb(24, 30, 54);
            btnShow.BackColor = Color.FromArgb(24, 30, 54);
        }

        private void btnMine_Click(object sender, EventArgs e)
        {
            pnlNav.Height = btnMine.Height;
            pnlNav.Top = btnMine.Top;
            pnlNav.Left = btnMine.Left;
            btnMine.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Mine());
        }

        private void btnMine_Leave(object sender, EventArgs e)
        {
            btnHome.BackColor = Color.FromArgb(24, 30, 54);
            btnMine.BackColor = Color.FromArgb(24, 30, 54);
        }

        private void btnAdmin_Click(object sender, EventArgs e)
        {
            pnlNav.Height = btnAdmin.Height;
            pnlNav.Top = btnAdmin.Top;
            pnlNav.Left = btnAdmin.Left;
            btnAdmin.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Admin());
        }

        private void btnAdmin_Leave(object sender, EventArgs e)
        {
            btnHome.BackColor = Color.FromArgb(24, 30, 54);
            btnAdmin.BackColor = Color.FromArgb(24, 30, 54);
        }

        private void btnSettings_Click(object sender, EventArgs e)
        {
            pnlNav.Height = btnSettings.Height;
            pnlNav.Top = btnSettings.Top;
            pnlNav.Left = btnSettings.Left;
            btnSettings.BackColor = Color.FromArgb(46, 51, 73);
            this.pnlMain.Controls.Clear();
            this.pnlMain.Controls.Add(new Settings());
        }

        private void btnSettings_Leave(object sender, EventArgs e)
        {
            btnHome.BackColor = Color.FromArgb(24, 30, 54);
            btnSettings.BackColor = Color.FromArgb(24, 30, 54);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tbSearch_Click(object sender, EventArgs e)
        {
            this.tbSearch.Text = "";
        }

        private void tbSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                MessageBox.Show("搜索不到任何结果，请重新输入!");
            }
        }

        //允许拖动窗口逻辑实现  https://www.cnblogs.com/techmango/archive/2012/03/31/2427523.html
        private bool isMouseDown = false;

        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置

        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = true;
                FormLocation = this.Location;
                mouseOffset = Control.MousePosition;
            }
        }

        private void FrmMain_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            int _x = 0;
            int _y = 0;
            if (isMouseDown)
            {
                Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;

                this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
            }
        }
    }
}